package Classes.Managers// ActionScript file
{
	import Classes.GameDatas.GD_Player;
	import Classes.GameUtils.GU_MsgDeal;
	import Classes.GameViews.ViewPanels.VP_OpenWinView;
	import Classes.GameViews.ViewPanels.VP_TextWinPanel;
	import Classes.Interfaces.I_SendTarget;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.PlatformInfo;
	import Classes.staticinfo.StaticInfo;
	
	import Encryption.Sex;
	
	import SimulateServer.ServerMain;
	
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.system.ApplicationDomain;
	import flash.utils.Timer;
	
	/**
	 *通讯类 
	 * @author 葛增文
	 * 
	 */	
	public class M_Communication extends Manager
	{
		private static var _instance:M_Communication;/*静态引用*/
		private var _communicationPath:String;/*通讯地址*/
		private var _nowSendObj:Object;/*当前发送对象*/
		private var _sendTarget:I_SendTarget;/*当前发送者*/
		//private var _isSending:Boolean;/*通讯标记*/
		//private var _extraObj:Object;/*额外的信息处理对象*/
		private var waiteTimer:Number=0;/*等待响应时间*/
		/**
		 * 是否需要响应时间跟token值验证 
		 */		
		private var isYanzhengComm:Boolean;
		public function M_Communication():void
		{
			_instance=this;
		}
		
		override public function importData():void
		{
			var severXmlList:XMLList=this.main.mainXML.commandAddr;
			var serverHeader:String=severXmlList.@serverHeader;
			var serverFooter:String=severXmlList.@serverFooter;
			var server:String;
			for each (var xml:XML in severXmlList.item)
			{
				server=xml.@server;
			}
			this._communicationPath=serverHeader+server+serverFooter;
			this.importOver();
		}
		
		/**
		 * 发送命令 
		 * @param msgType  命令号
		 * @param msgContent 内容	
		 * @param sendTarget 处理者
		 * @param _waiteTimer 命令隔多少时间过期
		 */		
		public function send(msgType:int,msgContent:Object=null,sendTarget:I_SendTarget=null,_waiteTimer:Number=0):void
		{
//			if(this.nowSendObj && this.nowSendObj.command==msgType)return;
			this.isYanzhengComm=this.isAddTimer(msgType);
//			if(this.nowSendObj && msgType!=127008 && msgType!=127009 && msgType!=12009 && msgType!=11002)return;
			if(this.nowSendObj && isYanzhengComm)return;
			if(isYanzhengComm)
			{
				this.waiteTimer=_waiteTimer==0?M_ConfigData.instance.waiteTimer:_waiteTimer;
			}else{
				this.waiteTimer=0;
			}
			
			var userID:int=msgType==1000?0:GD_Player.instance.playerID;
			if(null==msgContent){
				msgContent=new Object();
			}
			var d:Date=new Date();
			var tt:Number=d.time;
			msgContent.t=tt;
			
			msgContent	=	this.addpf(msgType,msgContent);		//加平台信息
			
			if(StaticInfo.key=="" || !this.isYanzhengComm)
			{
				msgContent.s=Sex.hash(String(tt+"2012416"));
			}else{
				msgContent.s=Sex.hash(String(tt+"2012416"+StaticInfo.key));
			}
			
//			trace("token传给后台="+StaticInfo.key);
//			msgContent.s=Sex.hash(String(tt+"2012416"+StaticInfo.key));
			msgContent.k=StaticInfo.key;
			
			this._nowSendObj={command:msgType,userID:userID,msgContent:msgContent};
			this._sendTarget=sendTarget;
			if(this.communicationPath=="null")
			{
				this.testCommunication();
			}
			else
			{
				this.URLCommunication();
			}
		}
		
		/**
		 *加平台 
		 * @param msgType
		 * @param msgContent
		 * @return 
		 * 
		 */
		private function addpf(msgType:int,msgContent:Object):Object
		{
			var ary:Array	=	M_ConfigData.instance.isAddPlatformCommandAry;//[128000, 128001, 128002, 11002, 127002, 127003, 127004, 127005, 127006, 127007];
			var isAdd:Boolean	=	false;
			for( var i:uint=0; i<ary.length; i++)
			{
				if(msgType==int(ary[i])){ isAdd=true;break;}
			}
			
			if(isAdd)msgContent.platform	=	PlatformInfo.instance.pf;
			
			return msgContent;
		}
		
		/**
		 *模拟测试通讯 
		 * 
		 */		
		private function testCommunication():void
		{
			ServerMain.instance.receive(JSON.encode(this.nowSendObj));
		}
		
		/**
		 *模拟测试通讯数据读取结束 
		 * @param getObj 读取的数据对象
		 * 
		 */		
		public function testLoadOverHandle(getStr:String):void
		{
			var dealObj:Object=JSON.decode(getStr)
			this.msgDeal(dealObj);
		}
		
		private var timer:Timer;
		
		/**
		 *网络通讯 
		 * 
		 */		
		private function URLCommunication():void
		{
			var pathStr:String;
			if(this.nowSendObj.path)
			{
				pathStr=this.nowSendObj.path;
			}
			else
			{
				pathStr=this.communicationPath;
			}
			var request:URLRequest=new URLRequest(pathStr);
			request.contentType="text/xml";
			request.method=URLRequestMethod.POST;
			var sendStr:String=JSON.encode(this.nowSendObj);
			request.data=sendStr;
			var loader:URLLoader=new URLLoader;
			loader.addEventListener(Event.COMPLETE,loadOverHandle);
			loader.load(request);
			
			timer=new Timer(this.waiteTimer,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,hanlderFun);
			if(this.waiteTimer>0)
			{
				timer.start();
			}
		}
		
		private function loadOverHandle(e:Event):void
		{
			(e.target as URLLoader).removeEventListener(Event.COMPLETE,loadOverHandle);
			var returnStr:String=String((e.target as URLLoader).data);
			var dealObj:Object=JSON.decode(returnStr);
			this.msgDeal(dealObj);
			
			if(timer)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER,hanlderFun);
				timer=null;
			}
		}
		
		private function hanlderFun(e:TimerEvent):void
		{
			if(timer)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE,hanlderFun);
				timer=null;
				VP_TextWinPanel.instance.openTextWindon(Language.raoluan,Language.refreshPage,refshPage);
			}
		}
		
		private function refshPage():void
		{
			ExternalInterface.call("enter");
		}
		
		/**
		 *消息处理 
		 * @param dealObj
		 * 
		 */		
		private function msgDeal(dealObj:Object):void
		{
			this._nowSendObj=null;
			GU_MsgDeal.dealMsg(dealObj,this.sendTarget);
			try{
				VP_OpenWinView.instance.closeWin();
			}catch(e:Error){
				trace("circle弹框没初始化");
			}
		}
		
		/**
		 * 是否需要验证 true为是     false 为false 
		 * @param comm
		 * @return 
		 * 
		 */		
		private function isAddTimer(comm:int):Boolean
		{
			var tmpCommArr:Array=M_ConfigData.instance.commandArr;
			var flag:Boolean=true;
			var len:int=tmpCommArr.length;
			for(var i:int=0;i<len;i++)
			{
				if(comm==int(tmpCommArr[i]))
				{
					flag=false;
					break;
				}
			}
			return flag;
		}
		
		/*getter & setter*/
		public static function get instance():M_Communication
		{
			return _instance;
		}
		
		public function get communicationPath():String
		{
			return _communicationPath;
		}
		
		public function get nowSendObj():Object
		{
			return _nowSendObj;
		}
		
		public function get sendTarget():I_SendTarget
		{
			return _sendTarget;
		}
		
		
	}
}